package cn.bookCity.serviceimpl;

import java.util.List;

import com.mchange.v2.async.StrandedTaskReporting;
import com.sun.org.apache.bcel.internal.generic.StackInstruction;

import cn.bookCity.Exception.MyException;
import cn.bookCity.dao.OrderDao;
import cn.bookCity.dao.OrderItemDao;
import cn.bookCity.dao.ProductDao;
import cn.bookCity.domain.Orderitem;
import cn.bookCity.domain.Orders;
import cn.bookCity.service.OrderService;
import cn.bookCity.util.BeanFactory;
import cn.bookCity.util.DataSourceUtil;

public class OrderServiceImpl implements OrderService {
	@Override
	public void addOrder(Orders orders)  {
		OrderDao orderDao=(OrderDao)BeanFactory.getBean("OrderDao");
		OrderItemDao orderItemDao=(OrderItemDao)BeanFactory.getBean("OrderItemDao");
		ProductDao p=(ProductDao)BeanFactory.getBean("ProductDao");
		try {
			DataSourceUtil.startTransaction();//开启事务
			orderDao.insertOrder(orders);
			for(Orderitem item:orders.getOrderitems()) {
				orderItemDao.InsertOrderItem(item);
			}
			p.updateProductNum(orders);
			DataSourceUtil.commitAndClose();
		}catch (Exception e) {
		    e.printStackTrace();
		    DataSourceUtil.rollbackAndClose();
		}
		
	}

	@Override
	public int getCount(String uid) {
		OrderDao orderDao=(OrderDao)BeanFactory.getBean("OrderDao");
		return orderDao.getCount(uid);
	}

	@Override
	public List<Orders> getPageOrders(int startNo, int pagesize,String uid) {
		OrderDao orderDao=(OrderDao)BeanFactory.getBean("OrderDao");
		return orderDao.getPageOrders(startNo, pagesize,uid);
	}

	@Override
	public List<Orderitem> getOrderItem(String id) {
		OrderItemDao orderItemDao=(OrderItemDao)BeanFactory.getBean("OrderItemDao");
		return orderItemDao.getOrderItems(id);
	}

	@Override
	public List<Orders> getOrders() {
		OrderDao orderDao=(OrderDao)BeanFactory.getBean("OrderDao");
		return orderDao.getOrders();
	}

	@Override
	public Orders getOrdersById(String id) {
		OrderDao orderDao=(OrderDao)BeanFactory.getBean("OrderDao");
		return orderDao.getOrdersById(id);
	}

}
